//
//  SearchShowView.m
//  search搜索标签
//
//  Created by 涂永江 on 2017/8/28.
//  Copyright © 2017年 涂永江. All rights reserved.
//

#import "SearchShowView.h"
#import <Masonry.h>
#import "HistoryViewCell.h"

@interface SearchShowView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *searchColltionView;

@property (nonatomic, strong) HistoryViewCell *cell;

@end

@implementation SearchShowView

-(UICollectionView *)searchColltionView
{
  
    if(_searchColltionView == nil) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
      
        
        _searchColltionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _searchColltionView.backgroundColor = [UIColor clearColor];
        _searchColltionView.pagingEnabled = YES;
        _searchColltionView.showsVerticalScrollIndicator = NO;
        _searchColltionView.showsHorizontalScrollIndicator = NO;
        
        [_searchColltionView registerNib:[UINib nibWithNibName:@"HistoryViewCell" bundle:nil] forCellWithReuseIdentifier:@"HistoryViewCell"];
        
        _searchColltionView.dataSource = self;
        _searchColltionView.delegate = self;
        
        
        [self addSubview:_searchColltionView];
        
        [_searchColltionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _searchColltionView;
    
    
}

-(void)reloadData
{

    [self.searchColltionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    if(self.dateSource && [self.dateSource respondsToSelector:@selector(numberOfSectionsInSearchShowView:)])
    {
       
        return [self.dateSource numberOfSectionsInSearchShowView:self];
    }else
    {
        return 1;
    }
    

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(self.dateSource && [self.dateSource respondsToSelector:@selector(numberOfItemsInHorizontalSearchShowView:)])
    {
        
        return [self.dateSource numberOfItemsInHorizontalSearchShowView:self];
    }else
    {
        return 0;
    }
    
   
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HistoryViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HistoryViewCell" forIndexPath:indexPath];
    if ([self.dateSource respondsToSelector:@selector(horizontalSearchShowView:titleForItemAtIndex:)])
    {
        cell.keyword = [self.dateSource horizontalSearchShowView:self titleForItemAtIndex:indexPath.row];
    }else
    {
    cell.keyword = @"";
    }
    

    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 10);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_cell == nil) {
        _cell = [[NSBundle mainBundle]loadNibNamed:@"HistoryViewCell" owner:nil options:nil][0];
    }
    
    if ([self.dateSource respondsToSelector:@selector(horizontalSearchShowView:titleForItemAtIndex:)])
    {
        _cell.keyword = [self.dateSource horizontalSearchShowView:self titleForItemAtIndex:indexPath.row];
    }else
    {
        _cell.keyword = @"";
    }
    

    return [_cell sizeForCell];
}


@end
